package com.fisher.checker.item;

import com.fisher.checker.DateUtils;
import com.fisher.checker.ErrorCode;
import com.fisher.checker.db.dao.ProductItemDao;
import com.fisher.checker.product.Product;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ProductItemContainer {
    private final List<ProductItem> productItemList = new ArrayList<>();
    private final List<String> productItemCodes = new ArrayList<>();
    private ProductItem currentProductItem;

    private int successCount;

    public List<ProductItem> getProductItemList() {
        return productItemList;
    }


    public ProductItem getCurrentProductItem() {
        return currentProductItem;
    }

    public ErrorCode setProductItem(String productItemCode, Product currentProduct, ProductItemDao dao) {
        if(productItemCode.length() < 20){
            return ErrorCode.ERR_BARCODE_FORMAT;
        }
        String brevityCode = this.getBrevityCode(productItemCode);
        if(!brevityCode.equals(currentProduct.getBrevityCode())){
            return ErrorCode.ERR_PRODUCT_NO;
        }

        boolean isDuplicated = false;
        try {
            isDuplicated = dao.doesProductExist(productItemCode);
        }catch (Exception e){
            e.printStackTrace();
            return ErrorCode.ERR_DB_ERR;
        }

        if(isDuplicated){
            return ErrorCode.ERR_PRODUCT_DUPLICATE;
        }

        currentProductItem = createProductItem(productItemCode, currentProduct);

        if(!currentProduct.isHasElectron()){
            productItemCodes.add(productItemCode);
            productItemList.add(currentProductItem);
            successCount++;
            currentProductItem.setSuccess(true);
        }
        return null;
    }

    private ProductItem createProductItem(String productItemCode, Product currentProduct) {
        ProductItem productItem = new ProductItem();
        Date date = new Date();
        productItem.setProductNo(currentProduct.getProductNo());
        productItem.setProductBarcode(productItemCode);
        productItem.setProductNm(currentProduct.getProductName());
        productItem.setDay(DateUtils.getCurrentDay());
        productItem.setTime(DateUtils.getTimeHMSM());
        productItem.setProjectNo(currentProduct.getProjectNo());
        return productItem;
    }

    public int getSuccessCount() {
        return successCount;
    }

    private String getBrevityCode(String code){
        return code.substring(0, 5);
    }

    public ErrorCode setElectronInfo(String eItemCode, Product currentProduct, ProductItemDao dao) {
        if(eItemCode.length() < 20){
            return ErrorCode.ERR_BARCODE_FORMAT;
        }

        String brevityCode = this.getBrevityCode(eItemCode);
        if(!brevityCode.equals(currentProduct.getElectron().getBrevityCode())){
            return ErrorCode.ERR_E_NO;
        }

        boolean isDuplicated = false;
        try {
            isDuplicated = dao.doesElectronPartExist(eItemCode);
        }catch (Exception e){
            e.printStackTrace();
            return ErrorCode.ERR_DB_ERR;
        }

        if(isDuplicated){
            return ErrorCode.ERR_E_ITEM_DUPLICATE;
        }

        productItemCodes.add(currentProductItem.getProductBarcode());
        productItemList.add(currentProductItem);

        Date date = new Date();
        currentProductItem.setElectronNo(currentProduct.getElectron().getElectronNo());
        currentProductItem.setElectronNm(currentProduct.getElectron().getElectronNm());
        currentProductItem.setElectronPartBarcode(eItemCode);
        currentProductItem.setDay(DateUtils.getCurrentDay());
        currentProductItem.setTime(DateUtils.getTimeHMSM());
        successCount++;
        currentProductItem.setSuccess(true);
        return null;
    }

}
